// ----------------
// Project:
// ESA Elevator
// ----------------
//
// ----------------
// Group: 163
// 2358569: Pascal Klink
// 2649456: Lukas Kreh
// ----------------
//
// Description:
// ----------------
// elevator display-interface
//
// Version History:
// ----------------
// 140116:

`timescale 1ns / 1ns

module display_if

          #(parameter FLOOR_BITS       =  7,
                      DISPLAY_SEGMENTS = 14)


          (input  wire [(FLOOR_BITS-1)      :0] CURRENT_FLOOR,

           output wire [(DISPLAY_SEGMENTS-1):0] ENABLE_SEGMENT);  // enable    

/* =============================INSERT CODE HERE======================================*/ 

	//Parameter to determine the number of digits used for the representation of the current floor in 7-segment
	localparam numberOfSegments = DISPLAY_SEGMENTS / 7;
	//Counting variables
	integer								i, k, l, m, n;
	//Registers used to store the BCD representation of the current floor
	reg[numberOfSegments * 4 - 1:0] 	bcdValue;
	//Helper register to store the 7-segment representation calculated from the BCD digit values
	reg[DISPLAY_SEGMENTS - 1:0]		segment;

	//Convert the unsigned int representation of the current floor to BCD
	always @ * begin
		for(i = 0; i < 2**FLOOR_BITS; i = i + 1) begin
			//Check which floor should be displayed
			if(i == CURRENT_FLOOR) begin
				k = 10;
				//Calculate each digit of the current floor to be represented
				for(l = 0; l < numberOfSegments; l = l + 1) begin
					//Example: value = 12623, 3rd-digit: (12623 % 1000 - 12623 % 100) / 100 = (623 - 23) / 100 = 600 / 100 = 6
					bcdValue[(l * 4) +: 4] = (i % k - i % (k / 10)) / (k / 10);
					k = k * 10;
				end
			end
		end
	end
	
	//Convert the BCD representation of the current floor to 7-segment code
	always @ * begin
		n = 0;
		//Go through each digit and convert it to the corresponding 7-segment code
		for(m = 0; m < numberOfSegments; m = m + 1) begin
			case(bcdValue[n +: 4])
				4'h0: segment[(m * 7) +: 7] = 7'b1111110;//0 
				4'h1: segment[(m * 7) +: 7] = 7'b0110000;//1 
				4'h2: segment[(m * 7) +: 7] = 7'b1101101;//2 
				4'h3: segment[(m * 7) +: 7] = 7'b1111001;//3 
				4'h4: segment[(m * 7) +: 7] = 7'b0110011;//4 
				4'h5: segment[(m * 7) +: 7] = 7'b1011011;//5 
				4'h6: segment[(m * 7) +: 7] = 7'b1011111;//6 
				4'h7: segment[(m * 7) +: 7] = 7'b1110000;//7 
				4'h8: segment[(m * 7) +: 7] = 7'b1111111;//8 
				4'h9: segment[(m * 7) +: 7] = 7'b1111011;//9 
				default: segment[(m * 7) +: 7] = 7'b1111110;//0 
			endcase
			n = n + 4;
		end
	end
	
	//Assign values of helper register to the real output
	assign ENABLE_SEGMENT = segment;

/* ====================================================================================*/
  
endmodule
